import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';
import 'package:xiaomi_shop/app/modules/user/controllers/user_controller.dart';
import 'package:xiaomi_shop/app/services/screenAdapter.dart';
import 'package:xiaomi_shop/app/widget/logoWidget.dart';

import '../controllers/register_step3_controller.dart';

class RegisterStep3View extends GetView<RegisterStep3Controller> {
  const RegisterStep3View({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          elevation: 0,
          actions: [
            TextButton(
                onPressed: () {},
                child: const Text(
                  "帮助",
                  style: TextStyle(color: Colors.black),
                ))
          ],
        ),
        body: ListView(
          padding: EdgeInsets.all(ScreenAdapter.width(40)),
          children: [
            LogoWidget(),
            SizedBox(
              height: ScreenAdapter.height(80),
            ),
            // 密码
            Container(
              margin: EdgeInsets.all(ScreenAdapter.width(40)),
              padding: EdgeInsets.all(ScreenAdapter.width(40)),
              height: ScreenAdapter.height(180),
              decoration: BoxDecoration(
                  color: Colors.grey.shade200,
                  borderRadius: BorderRadius.circular(10)),
              child: TextField(
                controller: controller.passwordTextController,
                keyboardType: TextInputType.phone,
                textInputAction: TextInputAction.done,
                textAlignVertical: TextAlignVertical.center,
                style: TextStyle(
                    fontSize: ScreenAdapter.fontSize(50),
                    fontWeight: FontWeight.bold),
                obscureText: true,
                decoration: InputDecoration(
                  hintText: "密码密码",
                  hintStyle: TextStyle(
                      fontSize: ScreenAdapter.fontSize(50),
                      fontWeight: FontWeight.normal),
                  border: OutlineInputBorder(borderSide: BorderSide.none),
                  // 可解决 TextField 文字不居中问题
                  contentPadding: EdgeInsets.symmetric(horizontal: 14.w),
                ),
              ),
            ),
            // 确认密码
            Container(
              margin: EdgeInsets.fromLTRB(ScreenAdapter.width(40), 0,
                  ScreenAdapter.width(40), ScreenAdapter.width(40)),
              padding: EdgeInsets.all(ScreenAdapter.width(40)),
              height: ScreenAdapter.height(180),
              decoration: BoxDecoration(
                  color: Colors.grey.shade200,
                  borderRadius: BorderRadius.circular(10)),
              child: TextField(
                controller: controller.enterPasswordTextController,
                textInputAction: TextInputAction.done,
                textAlignVertical: TextAlignVertical.center,
                style: TextStyle(
                    fontSize: ScreenAdapter.fontSize(50),
                    fontWeight: FontWeight.bold),
                obscureText: true,
                decoration: InputDecoration(
                  hintText: "确认密码",
                  hintStyle: TextStyle(
                      fontSize: ScreenAdapter.fontSize(50),
                      fontWeight: FontWeight.normal),
                  border: const OutlineInputBorder(borderSide: BorderSide.none),
                  // 可解决 TextField 文字不居中问题
                  contentPadding: EdgeInsets.symmetric(horizontal: 14.w),
                ),
              ),
            ),

            // 登录
            Container(
              margin: EdgeInsets.only(
                  top: ScreenAdapter.height(20),
                  left: ScreenAdapter.height(80),
                  right: ScreenAdapter.height(80)),
              height: ScreenAdapter.height(136),
              child: ElevatedButton(
                  style: ButtonStyle(
                      backgroundColor: MaterialStatePropertyAll(Colors.red),
                      foregroundColor: MaterialStatePropertyAll(Colors.white),
                      textStyle: MaterialStatePropertyAll(TextStyle(
                          fontSize: ScreenAdapter.fontSize(40),
                          fontWeight: FontWeight.bold))),
                  onPressed: () async {
                    if (controller.passwordTextController.text.isNotEmpty &&
                        controller
                            .enterPasswordTextController.text.isNotEmpty) {
                      if (controller.passwordTextController.text ==
                          controller.enterPasswordTextController.text) {
                        bool result = await controller.register();
                        if (result) {
                          print("注册成功，并登录");
                          // 更新用户信息
                          UserController userController =
                              Get.find<UserController>();
                          userController.getUserInfo();
                          // 注册成功，跳转至个人中心
                          Get.back();
                        } else {
                          Get.snackbar("提示信息", "注册失败");
                        }
                      } else {
                        Get.snackbar("提示信息", "两次输入的密码不一致");
                      }
                    } else {
                      Get.snackbar("提示信息", "密码不能为空");
                    }
                  },
                  child: const Text("登录")),
            ),
          ],
        ));
  }
}
